package cn.dakaqi.sys.topic.services;

import cn.dakaqi.entities.Organization;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/6/14.
 */
@Component(value = "organizationService")
public class OrganizationServiceImpl implements OrganizationService
{
    @Autowired
    OrganizationDao organizationDao;

    @Override
    public List<Organization> findByName(String name, int pageNumber) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("orgname", new SearchFilter("orgname", SearchFilter.Operator.LIKE, name));
        Specification<Organization> spec = DynamicSpecifications.bySearchFilter(filters.values(), Organization.class);
        return this.organizationDao.findAll(spec);
    }

    @Override
    public Organization findByOnePass(String onePass) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("onepass", new SearchFilter("onepass", SearchFilter.Operator.EQ, onePass));
        Specification<Organization> spec = DynamicSpecifications.bySearchFilter(filters.values(), Organization.class);
        List<Organization> list = this.organizationDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }
}
